-- 保存锁屏前的音量
local savedVolume = nil

local function systemWatcherCallback(eventType)
	local output = hs.audiodevice.defaultOutputDevice()
	if eventType == hs.caffeinate.watcher.screensDidLock then
		-- 锁屏：保存当前音量并静音
		savedVolume = output:volume()
		output:setMuted(true)
		hs.timer.doAfter(0.5, function()
			hs.execute("pmset displaysleepnow")
		end)
	elseif eventType == hs.caffeinate.watcher.screensDidUnlock then
		-- 解锁：恢复音量（如果之前保存过）
		if savedVolume ~= nil then
			output:setMuted(false)
			output:setVolume(savedVolume)
			savedVolume = nil
		end
	end
end

-- 启动 watcher
systemWatcher = hs.caffeinate.watcher.new(systemWatcherCallback)
systemWatcher:start()
